import { TableColumnObject } from 'amis/lib/renderers/Table'
import lodash from 'lodash'
import captchaApplicationAdminService from '@/core/captcha/generated/service/admin/captchaApplicationAdminService'

class CaptchaAppIdColumnUtils {
	getSchema = (parmas?: Partial<TableColumnObject>): TableColumnObject => {
		const name = parmas?.name ? parmas.name : 'captchaAppId'
		const label = '图形验证码应用'
		const mergeSchema = Object.assign(
			{
				type: 'service',
				name: name,
				label: label,
				dataProvider: (data: any, setData: Function) => {
					const id = data[name]
					captchaApplicationAdminService.getByIdsWithDebounce(id).then(list => {
						let value = '-'
						lodash.forEach(list, item => {
							if (item.id === id) {
								value = item.title
							}
						})
						setData({ title: value })
					})
				},
				body: {
					type: 'tpl',
					tpl: '${title}',
				},
			},
			parmas,
		)

		const schema = mergeSchema
		return schema as TableColumnObject
	}
}

export default new CaptchaAppIdColumnUtils()
